
import 'package:app/generated/app_data.dart';
import 'package:app/utils/shared_preferences_util.dart';
import 'package:flutter_jpf_core/util/string_util.dart';
import 'package:flutter_jpf_core/extension/map_extension.dart';


class UserModel {

  //账号
  String account;
  ///密码
  String userPassword;
  ///登陆成功返回token
  String token = "";
  ///登陆成功返回secret
  String secret = "";
  ///用户名
  String userName = "";

  String headPictureUrl = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fattach.bbs.miui.com%2Fforum%2F201310%2F19%2F235439yh04c010wm0qrk5d.jpg&refer=http%3A%2F%2Fattach.bbs.miui.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1615424735&t=3d6ca308c728711da52eb4b4e08c1b1e";


  Map otherInfo = {};

  static UserModel _currentUser;

  //获取当前用户信息
  static UserModel getCurrentUser() {
    if (_currentUser == null) {
      UserModel user = SharedPreferencesUtil.getUser();
      _currentUser = user;
      if (_currentUser == null) {
        _currentUser = UserModel();
      }
    }
    return _currentUser;
  }

  static void setTempUser(UserModel userModel){
    _currentUser = userModel;
  }

  /// 清空 User 对象
  static clear() {
    if(_currentUser != null){
      _currentUser.userPassword = "";
      _currentUser.token = "";
      _currentUser.secret = "";
      SharedPreferencesUtil.saveUser(_currentUser);
    }
    _currentUser = null;
    AppData.getInstance().clearUserData();
  }


  UserModel({this.account ,this.token = "",this.userName ,this.userPassword });

  bool isVaild() {
    return !StringUtils.isEmpty(token);
  }

  UserModel.fromJson(Map json) {
    account = json.strValue("account");
    secret = json.strValue("secret");
    token = json.strValue("token");
    userPassword = json.strValue("userPassword");
    userName = json.strValue("name");
  }

  Map toJSON() {
    final Map<String, dynamic> map = new Map<String, dynamic>();
    map["account"] = this.account;
    map["userPassword"] = this.userPassword;
    map["token"] = this.token;
    map["secret"] = this.secret;
    map["userName"] = this.userName;;
    return map;
  }


//获取其他属性

}